package com.huike.report.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huike.common.core.domain.model.LoginUser;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private TbContractMapper contractMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;
    
    @Autowired
    private ReportMapper reportMpper;


    @Override
    public IndexVo getIndex(IndexStatisticsVo request) {
        Long deptId= request.getDeptId();
        TbAssignRecord tbAssignRecord=new TbAssignRecord();
        tbAssignRecord.setLatest("1");
        assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        return null;
    }

    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
        List<Map<String, Object>> list= contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId= (Long) datum.get("dept_id");
            if(deptId!=null){
                SysDept dept= deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
        }
        return list;
    }

    @Override
    public Map<String, Integer> getVulnerabilityMap(IndexStatisticsVo vo) {
        Map<String, Integer> map = new HashMap<>();
        //线索数
       Integer cluesNums = clueMapper.countAllCluesNums(vo);
       map.put("cluesNums",cluesNums);
       //有效线索数
       Integer effectiveCluesNums=clueMapper.countAllEffectiveCluesNums(vo);
       map.put("effectiveCluesNums",effectiveCluesNums);
        //商机数
       Integer businessNums=businessMapper.countAllBusinessNums(vo);
       map.put("businessNums",businessNums);
       //合同数
       Integer contractNums=contractMapper.countAllContractNums(vo);
       map.put("contractNums",contractNums);

        return map;
    }

    @Override
    public IndexVo getIndexVo(IndexStatisticsVo vo) {
        String date = DateUtils.getDate();
        String username = SecurityUtils.getUsername();

        // 线索数量
        Integer cluesNum = clueMapper.countCluesNums(vo,username);
        //今日新增线索数量
        Integer todayCluesNum = clueMapper.todayCluesNums(date,username);
        //商机数量
        Integer businessNum=businessMapper.countBusinessNums(vo,username);
        //今日新增商机数量
        Integer todayBusinessNum=businessMapper.todayBusinessNums(date,username);
        //客户数量
        Integer contractNum=contractMapper.countContractNums(vo,username);
        //今日新增客户数量
        Integer todayContractNum=contractMapper.todayContractNums(date,username);
        //销售金额
        float salesAmount=contractMapper.sumSalesAmount(vo,username);
        BigDecimal bigDecimal = new BigDecimal(salesAmount);
        double salesAmounts = bigDecimal.doubleValue();
        //今日新增销售额
        float todaySalesAmount= contractMapper.todaySumSalesAmount(date,username);
        double todaySalesAmounts = new BigDecimal(todaySalesAmount).doubleValue();
        //待跟进线索数量
       Integer tofollowedCluesNum= clueMapper.countTofollowedCluesNum(username);
       //待分配线索
       Integer toallocatedCluesNum= clueMapper.countToallocatedCluesNum(username);
       //待跟进商机数量
       Integer tofollowedBusinessNum=businessMapper.countTofollowedBusinessNum(username);
       //待分配商机
       Integer toallocatedBusinessNum=businessMapper.countToallocatedBusinessNum(username);

        IndexVo indexVo = new IndexVo();
        indexVo.setContractNum(contractNum);
        indexVo.setBusinessNum(businessNum);
        indexVo.setCluesNum(cluesNum);
        indexVo.setTodayBusinessNum(todayBusinessNum);
        indexVo.setTodayCluesNum(todayCluesNum);
        indexVo.setTodayContractNum(todayContractNum);
        indexVo.setSalesAmount(salesAmounts);
        indexVo.setTodaySalesAmount(todaySalesAmounts);
        indexVo.setTofollowedCluesNum(tofollowedCluesNum);
        indexVo.setTofollowedBusinessNum(tofollowedBusinessNum);
        indexVo.setToallocatedCluesNum(toallocatedCluesNum);
        indexVo.setToallocatedBusinessNum(toallocatedBusinessNum);
        return indexVo;
    }

    @Override
    public Map<String, String> getBusinessChangeStatistics(IndexStatisticsVo vo) {
        return null;
    }

    @Override
    public Map<String, String> getSalesStatistic(IndexStatisticsVo vo) {

        return null;
    }


    private BigDecimal getRadio(Integer all,Long num) {
        if(all.intValue()==0){
            return new BigDecimal(0);
        }
		BigDecimal numBigDecimal = new BigDecimal(num);
		BigDecimal allBigDecimal = new BigDecimal(all);
		BigDecimal divide = numBigDecimal.divide(allBigDecimal,4,BigDecimal.ROUND_HALF_UP);
		return divide.multiply(new BigDecimal(100));
	}

}